﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Boxi.Extensions;

namespace Boxi
{
    public partial class Settings : PhoneApplicationPage
    {
        private const String GameKey = "Game";
        private const String AccentColorKey = "Accent Color";
        private const String ComplimentaryColorKey = "Accent Opposite";

        public Settings()
        {
            InitializeComponent();

        }
        private Color DefaultAccentColor
        {
            get
            {
               return (Color)Application.Current.Resources["PhoneAccentColor"];
            }
        }

        private Color DefaultSecondaryAccentColor
        {
            get
            {
                return DefaultAccentColor.GetComplimentaryColor();
            }
        }

        private void btnStartGame_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
        private void cbxSound_Click(object sender, RoutedEventArgs e)
        {
            if (cbxSound == null)
                return;
            PersistentSettings.Instance.IsSoundEnabled = cbxSound.IsChecked.Value;
        }

        private void cbxVibration_Click(object sender, RoutedEventArgs e)
        {
            if (cbxVibration == null)
                return;
            PersistentSettings.Instance.IsVibrationEnabled = cbxVibration.IsChecked.Value;
        }

        private void lbxPlayer1Color_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxPlayer1Color == null)
            return;

            var colorString = ((TextBlock) ((ListBoxItem) lbxPlayer1Color.SelectedItem).Content).Text;
            PersistentSettings.Instance.AccentColor = ChangeListBoxSelectedItem(colorString, lbxPlayer1Color);
        }

        private void lbxPlayer2Color_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxPlayer2Color == null)
                return;

            var colorString = ((TextBlock)((ListBoxItem)lbxPlayer2Color.SelectedItem).Content).Text;
            PersistentSettings.Instance.SecondaryAccentColor = ChangeListBoxSelectedItem(colorString, lbxPlayer2Color);

            //var colorString = ((TextBlock)((ListBoxItem)lbxPlayer2Color.SelectedItem).Content).Text;
            //PersistentSettings.Instance.SecondaryAccentColor = Helper.GetColor(colorString);

            //foreach (ListBoxItem colorOption in lbxPlayer2Color.Items)
            //{
            //    colorOption.Opacity = (colorOption.IsSelected) ? 1d : 0.3d;
            //    colorOption.Margin = new Thickness((colorOption.IsSelected) ? 1d : 4d);
            //    colorOption.Padding = new Thickness((colorOption.IsSelected) ? 4d : 1d);
            //    ((TextBlock)colorOption.Content).Foreground = new SolidColorBrush((colorOption.IsSelected) ? Colors.Black : Colors.White);
            //    ((TextBlock)colorOption.Content).FontWeight = (colorOption.IsSelected) ? FontWeights.Bold : FontWeights.Normal;
            //}
        }
        private void AddColorToListBox(String colorName, Color colorToAdd, Color selectedColor, ListBox listBox)
        {
            var lbi = new ListBoxItem();
            listBox.Items.Add(lbi);
            lbi.Padding = new Thickness(4);
            lbi.Background = new SolidColorBrush(colorToAdd);
            var tb = new TextBlock();
            lbi.Content = tb;
            tb.Text = colorName;

            if (colorToAdd == selectedColor)
            {
                lbi.Opacity = 1d;
                lbi.IsSelected = true;
                lbi.Margin = new Thickness(1);
                lbi.Padding = new Thickness(4);
                tb.Foreground = new SolidColorBrush(Colors.Black);
                tb.FontWeight = FontWeights.Bold;
            }
            else
            {
                lbi.Opacity = 0.3d;
                lbi.Margin = new Thickness(4);
                lbi.Padding = new Thickness(1);
                tb.Foreground = new SolidColorBrush(Colors.White);
                tb.FontWeight = FontWeights.Normal;
            }
        }
        private Color ChangeListBoxSelectedItem(String colorName,  ListBox listBox)
        {
            foreach (ListBoxItem colorOption in listBox.Items)
            {
                colorOption.Opacity = (colorOption.IsSelected) ? 1d : 0.3d;
                colorOption.Margin = new Thickness((colorOption.IsSelected) ? 1d : 4d);
                colorOption.Padding = new Thickness((colorOption.IsSelected) ? 4d : 1d);
                ((TextBlock)colorOption.Content).Foreground = new SolidColorBrush((colorOption.IsSelected) ? Colors.Black : Colors.White);
                ((TextBlock)colorOption.Content).FontWeight = (colorOption.IsSelected) ? FontWeights.Bold : FontWeights.Normal;
            }

            if (colorName == AccentColorKey)
                return DefaultAccentColor;
            else if (colorName == ComplimentaryColorKey)
                return DefaultSecondaryAccentColor;
            else
                return Helper.GetColor(colorName);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            AddColorToListBox(AccentColorKey, DefaultAccentColor, PersistentSettings.Instance.AccentColor, lbxPlayer1Color);
            AddColorToListBox(ComplimentaryColorKey, DefaultSecondaryAccentColor, PersistentSettings.Instance.SecondaryAccentColor, lbxPlayer2Color);
            foreach (var color in typeof(Colors).GetProperties())
            {
                if (!"BlackWhiteTransparentDarkGrayLightGray".Contains(color.Name))
                {
                    AddColorToListBox(color.Name, Helper.GetColor(color.Name), PersistentSettings.Instance.AccentColor, lbxPlayer1Color);
                    AddColorToListBox(color.Name, Helper.GetColor(color.Name), PersistentSettings.Instance.SecondaryAccentColor, lbxPlayer2Color);
                }
            }
#if (DEBUG)
            btnResetSettings.Visibility = Visibility.Visible;
            btnResetGame.Visibility = Visibility.Visible;
#endif

        }

        private void btnResetSettings_Click(object sender, RoutedEventArgs e)
        {
            PersistentSettings.Instance.Reset();
        }

        private void btnResetGame_Click(object sender, RoutedEventArgs e)
        {
           if (PhoneApplicationService.Current.State.ContainsKey(GameKey))
               PhoneApplicationService.Current.State.Remove(GameKey);
            
           if (IsolatedStorageSettings.ApplicationSettings.Contains(GameKey))
               IsolatedStorageSettings.ApplicationSettings.Remove(GameKey);
        }

    }
}